home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / examples / task_end.c < prev    next >
C/C++ Source or Header  |  1997-07-22  |  5KB  |  163 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: task_end.c,v 1.4 1997/07/09 13:26:15 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. /*
  34.  *    Filename:     task_end.c
  35.  *
  36.  * This program will tell a task0 instance to terminate.
  37.  * This is done by seeking the "known" service from a mailbox,
  38.  * initiating communication telling the task to terminate itself.
  39.  * The message mailbox provides the communication information
  40.  * to the specified task0 instance.
  41.  *
  42.  *    usage: task_end [service_name] [index_nbr]
  43.  *
  44.  *  files used:  task_end.c taskf.c
  45.  */
  46.  
  47. #include <stdio.h>
  48. #ifndef WIN32
  49. #include <unistd.h>        /* for gethostname */
  50. #else
  51. #include "pvmwin.h"
  52. #endif
  53. #include "pvm3.h"
  54.  
  55. main( argc, argv )
  56. int argc;
  57. char *argv[];
  58. {
  59.     char *me = "task_end";
  60.     char machine[25];        /* local machine that task1 is running on */
  61.     int mytid;                /* tid of task1 */
  62.     int my_context;            /* context of task1 - itself */
  63.  
  64.     int index;                /* index of the service seeking */
  65.     char *service_name;        /* name of the service seeking */
  66.  
  67.     int their_context;        /* context of the service's owner */
  68.                             /*   - comm on this context */
  69.     int msg_buf;            /* buffer to store mailbox's message */
  70.     int their_tid;            /* tid "offering" the desired service */
  71.     char server[25];        /* name of the machine "offering" service */
  72.  
  73.     char msg_txt[100];        /* message to send to service */
  74.  
  75.     /* display_incomming_parameters( me, argc, argv ); */
  76.  
  77.     printf( "\n" );
  78.     if ( argc != 3 ) {
  79.         printf( "\nusage: task_end [service_name] [index_nbr]\n" );
  80.         exit( -1 );
  81.     }
  82.  
  83.     index = atoi( argv[2] );
  84.     service_name = argv[1];
  85.  
  86.     printf( "Looking for service name <%s> with index number [%d].\n",
  87.             service_name, index );
  88.  
  89.     if ( (mytid = pvm_mytid()) == PvmSysErr ) {
  90.         printf( "\nPVM not up!\n" );
  91.         exit( -1 );
  92.     }
  93.  
  94.     gethostname( machine, 25 );
  95.  
  96.     printf( "%s: t%x on machine <%s> with context %d.\n",
  97.             me, mytid, machine, pvm_getcontext() );
  98.  
  99.     /*
  100.      *  look for the user specified service name/index pair
  101.      */
  102.     msg_buf = pvm_recvinfo( service_name, index, PvmMboxDefault );
  103.     if ( msg_buf >= 0 ) {
  104.         pvm_setrbuf( msg_buf );
  105.         pvm_upkint( &their_tid, 1, 1 );
  106.         pvm_upkint( &their_context, 1, 1 );
  107.         pvm_upkstr( server );
  108.         printf( "service name <%s> of index %d on <%s> id: %x   ",
  109.                 service_name, index, server, their_tid );
  110.         printf( "with their context: %d\n", their_context );
  111.     }
  112.     else {
  113.         switch ( msg_buf )
  114.         {
  115.             case PvmNotFound:
  116.                 printf( "\n%s: no service running\n", me );
  117.                 break;
  118.             case PvmBadParam:
  119.                 printf( "\n%s: Invalid argument to pvm_recvinfo().\n",
  120.                         me );
  121.                 break;
  122.             case PvmNoSuchBuf:
  123.                 printf( "\n%s: Message buffer id does not exist.\n",
  124.                         me );
  125.                 break;
  126.             case PvmDenied:
  127.                 printf(
  128.                     "\n%s: Key locked by another task, can't delete.\n",
  129.                         me );
  130.                 break;
  131.             default:
  132.                 lpvmerr( "\n%s: task_end.c: error %d", me, msg_buf );
  133.                 break;
  134.         } /* end_switch */
  135.         exit( -1 );
  136.     }
  137.  
  138.     /*
  139.      *  send END message to the service with their context set
  140.      */
  141.  
  142.     /* activate the server's context */
  143.     my_context = pvm_setcontext( their_context );
  144.  
  145.     printf( "%s: t%x on machine <%s> with context %d.\n",
  146.             me, mytid, machine, pvm_getcontext() );
  147.     sprintf( msg_txt, "END" );
  148.     pvm_initsend( PvmDataDefault );
  149.     pvm_pkstr( msg_txt );
  150.     pvm_send( their_tid, 1 );
  151.  
  152.     /*
  153.      *  reset back to my original context
  154.      */
  155.     pvm_setcontext( my_context );
  156.     printf( "%s: t%x on machine <%s> with context %d.\n",
  157.             me, mytid, machine, pvm_getcontext() );
  158.  
  159.     pvm_exit();
  160.     exit( 0 );
  161. }
  162.  
  163.